/**
 * Company ule.com
 * Copyright (C) 2009-2016 All Rights Reserved.
 */
package com.runmi.webchat.aftersale.dbcore.config;


import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
//import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.annotation.MapperScan;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;


/**
 * @author luojianhui
 * @version $Id MybatisConfiguration.java, v 0.1 2016-10-26 16:09 luojianhui Exp $$
 */
@Configuration
@ConditionalOnClass({EntityManager.class })
@EnableTransactionManagement
@Slf4j
@MapperScan(basePackages={"com.runmi.webchat.aftersale.dbcore.mapper","tk.mybatis.mapper.common.Mapper"},sqlSessionTemplateRef  = "sqlSessionTemplate")
public class MybatisConfiguration {

    //private RelaxedPropertyResolver propertyResolver;

    @Resource(name="dataSource")
    public DataSource dataSource;
//
//    public void setEnvironment(Environment environment) {
//        this.propertyResolver = new RelaxedPropertyResolver(environment,"mybatis.");
//    }


    @Bean(name="sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() {
        try {

            SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
            sessionFactory.setDataSource(dataSource);
            sessionFactory.setTypeAliasesPackage("com.runmi.webchat.aftersale.dbcore.entity;");
            sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath*:mybatis/mappers/*Mapper.xml"));
            sessionFactory.setConfigLocation(new DefaultResourceLoader()
                    .getResource("mybatis/mybatis-config.xml"));
            return sessionFactory.getObject();
        } catch (Exception e) {
            log.error("Could not confiure mybatis session factory",e);
            return null;
        }
    }

    @Bean(name = "sqlSessionTemplate")
    public SqlSessionTemplate mdmSqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }



    //    @Bean
//    public PlatformTransactionManager txManager() {
//        return new DataSourceTransactionManager(dataSource);
//    }
    @Bean(name="transactionManager")
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}